package com.ctg.behavior.calc.impala.builder.addictions;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.impala.report.addictions.CtAddictionsTimesReport;
import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.common.addictions.CtAddictionsCalcData;
import com.ctg.behavior.common.addictions.CtAddictionsCell;
import com.ctg.behavior.common.addictions.CtAddictionsRow;
import com.ctg.behavior.common.calc.CtCalcPropertyType;
import com.ctg.behavior.common.consts.CtFieldConsts;
import com.ctg.behavior.utils.CtDoubleUtils;

@Component
public class CtAddictionsTimesResultBuilder {

	// KEY:
	private static ThreadLocal<Map<String, CtAddictionsRow>> tlIndexRows = new ThreadLocal<>();
//	// KEY:
//	private static ThreadLocal<Map<String, CtAddictionsCell>> tlIndexCells = new ThreadLocal<>();

	public void resetResultBuilderTheadLocal() {
		Map<String, CtAddictionsRow> indexedRows = new HashMap<>();
		tlIndexRows.set(indexedRows);
//		Map<String, CtAddictionsCell> indexedCells = new HashMap<>();
//		tlIndexCells.set(indexedCells);
	}

	@Autowired
	private CtAddictionsRowKeyBuilder rowKeyBuilder;

	public CtAddictionsCalcData doBuilder(CtAddictionsCalcData calcData, List<LinkedHashMap<String, Object>> calcResults, CtAddictionsTimesReport report) {

		// 分组属性字段
		CtCalcPropertyType byProperty = CtCalcPropertyUtils.getValueType(report.getByField());

		// List<LinkedHashMap<String, Object>> calcResult
		for (LinkedHashMap<String, Object> oneResult : calcResults) {

			String rowKey = rowKeyBuilder.buildRowKey(byProperty, oneResult);
			CtAddictionsRow tlRow = tlIndexRows.get().get(rowKey);
			// 初始化ROW
			if (null == tlRow) {
				tlRow = new CtAddictionsRow();
				tlIndexRows.get().put(rowKey, tlRow);
				// 初始化ROW中的cells
				for (int bucketIndex = 0; bucketIndex <= report.getBuckets().size(); bucketIndex++) {
					CtAddictionsCell cell = new CtAddictionsCell();
					if (bucketIndex > 0) {
						cell.setBucketStart(Double.valueOf(report.getBuckets().get(bucketIndex-1)));
					} else {
						cell.setBucketStart(1D);
					}
					if (bucketIndex < report.getBuckets().size()) {
						cell.setBucketEnd(Double.valueOf(report.getBuckets().get(bucketIndex)));
					} else {
						Double maxValue = Double.valueOf(report.getBuckets().get(bucketIndex-1));
						cell.setBucketEnd(maxValue*100);
					}
					tlRow.getCells().add(cell);
//					String cellKey = rowKeyBuilder.buildRowCellKey(rowKey, bucketIndex);
//					tlIndexCells.get().put(cellKey, cell);
				}
				calcData.getRows().add(tlRow);
			}
			int bucketIndex = Integer.valueOf(oneResult.get("bucketIndex").toString());
			Long cellPeople = Long.valueOf(oneResult.get(CtFieldConsts.FIELD_KEY_TOTAL).toString());
			// 填充ROW中的ByValue
			if (!StringUtils.isEmpty(byProperty.getProperty())) {
				tlRow.setByValue(oneResult.get(byProperty.getProperty()).toString());
			} else {
				tlRow.setByValue(oneResult.get(CtFieldConsts.FIELD_KEY_EVENT_DATE).toString());
			}
			// 累加到对应ROW的totalPeople
			tlRow.setTotalPeople(tlRow.getTotalPeople() + cellPeople);
			// 填充Cell的people
			tlRow.getCells().get(bucketIndex).setPeople(cellPeople);
			// 计算Cell中的百分占比
			for (CtAddictionsCell cell : tlRow.getCells()) {
				String percent = CtDoubleUtils.percent2Scale(cell.getPeople(), tlRow.getTotalPeople());
				cell.setPercent(Double.valueOf(percent));
			}
		}

		return calcData;
	}

}
